package top.revery.task.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import top.revery.api.api.user.IUserWorksApi;
import top.revery.api.config.UserPageData;
import top.revery.api.entity.user.BaseUserKol;
import top.revery.api.entity.user.BaseUserWorks;
import top.revery.api.entity.vo.BaseUserWorksVo;
import top.revery.task.dto.KolDto;
import top.revery.task.dto.UserWorksDto;
import top.revery.task.service.UserWorksService;

import java.util.*;

/**
 * @author denghao
 * @title   作品
 * @package
 * @date 2019/12/13 17:57
 */
@Slf4j
@RestController
@Transactional
public class  UserWorksController implements IUserWorksApi {

    @Autowired
    private UserWorksDto userWorksDto;

    @Autowired
    private UserWorksService userWorksService;
    @Autowired
    private KolDto kolDto;

    //保存作品
    @Override
    public BaseUserWorks save(BaseUserWorks baseUserWorks) {
            return userWorksDto.save(baseUserWorks);
    }


    //分页多条件查询所有
    @Override
    public UserPageData getList(int pageNum, int pageSize, Map map) {
        Page<BaseUserWorks> pageList = userWorksService.findPage(pageNum, pageSize, map);
        List<BaseUserWorks> content = pageList.getContent();
        List<BaseUserWorksVo> list = new ArrayList<>();
        for (BaseUserWorks baseUserWorks:content){
            BaseUserKol baseUserKol = kolDto.findById(baseUserWorks.getRedId()).get();
            BaseUserWorksVo baseUserWorksVo = new BaseUserWorksVo();
            baseUserWorksVo.setBaseUserKol(baseUserKol);
            baseUserWorksVo.setBaseUserWorks(baseUserWorks);
            list.add(baseUserWorksVo);
        }
        UserPageData userPageData = new UserPageData();
        userPageData.list=list;
        userPageData.rows = pageList.getTotalElements();
        return userPageData;
    }

    //删除
    @Override
    public void delete(Long id) {

        try {
           userWorksDto.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
  //上传视频
   /* @Override
    public String upload(Long redId, Long taskId, MultipartFile file,HttpServletRequest request)throws Exception {
        if (file.isEmpty()) {
            throw new Exception("文件不能为空");
        }
        BaseUserWorks baseUserWorks = new BaseUserWorks();

      //  DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
      //  String typeName = "";

       // String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
        String fileDateName = file.getOriginalFilename() ;
        log.info("上传的文件名为：" + fileDateName);

        String filePath = "";
        String os = System.getProperty("os.name");
        Calendar c = Calendar.getInstance();
      //  String dateName = df.format(c.getTime());
        Date date = new Date();
        if(os.toLowerCase().startsWith("win")){
            //windows 下路径
            filePath="E://shipin/"+(new SimpleDateFormat("yyyyMM")).format(date) + "/" + (new SimpleDateFormat("dd")).format(date) + "/";
         //  filePath = "E://shipin/"+dateName.substring(0,10)+"/";
        } else{
            //linux 下路径
            filePath = "/shipin"+(new SimpleDateFormat("yyyyMM")).format(date) + "/" + (new SimpleDateFormat("dd")).format(date) + "/";
        }
        File newFile = new File(filePath);
        if(!newFile.exists()){
            newFile.mkdirs();
        }
        filePath += fileDateName;
        String fileUrl= IOUtils.fileUpload(file, filePath);
        baseUserWorks.setRedId(redId);
        baseUserWorks.setTaskId(taskId);
        baseUserWorks.setAddress(fileUrl);
        baseUserWorks.setAddTime(new Timestamp(new Date().getTime()));
        userWorksDto.save(baseUserWorks);

       *//* String url =   request.getScheme()+"://"+request.getServerName()+":"+
                request.getServerPort()+"/"+filePath;*//*
        return filePath;

    }*/


}
